/**
 * Description:该类主要用于保存查询数据
 * Copyright: 成都和为科技有限公司 2012
 */
function DataService() {
}

DataService.inherit(Control);

DataService.prototype.init = function () {
    DataService.base.init.call(this);
}

DataService.prototype.initLoad = function() {
	this._param = new Object();
	this._url = "";
    this._action = null; //Action, Data方式提交
    this._aData = new Object();
    this._aData.records = new Array();
    this._control = null; //关联控件
}

/**
 * 获得请求地址
 */
DataService.prototype.getURL = function(){
	return this._url;
}

/**
 * 设置请求地址
 */
DataService.prototype.setURL = function(url){
    //支持action, data方式提交数据
    if (url.split(":").length > 1) {
        this._url = url.split(":")[0];
        this._action = url.split(":")[1];
    } else {
        this._url = url;
    }
}
/**
 * 设置关联控件
 * @param oControl
 */
DataService.prototype.setControl = function(oControl) {
    this._control = oControl;
}
/**
 * 设置数据查询结果数据
 * @param arrData JSON数组
 */
DataService.prototype.setData = function(arrData) {
    this._aData = arrData;
}

DataService.prototype.getData = function() {
    return this._aData;
}

DataService.prototype.addData = function(oData) {
    this._aData.push(oData);
}

DataService.prototype.removeData = function(index) {
    this._aData.splice(index, 1);
}

/**
 * Commit data to data server
 * @param oData，json格式数据
 * @return	提交的返回XML
 */
DataService.prototype.commitData = function(oData, toBody) {
    var soap = new HWSOAP(this._url);
    if (toBody) {
        soap.sendb(oData);
    } else {
        soap.send(oData);
    }
    var oResult = soap.getResult();
    //出错
    if (oResult && !oResult.status) {
    	soap.showError(oResult.error);
    }
   	return oResult;
}

/**
 * 异步提交结果
 * @param {} oData
 * @param {} oCallBack 回调方法
 */
DataService.prototype.commitDataAsync = function(oData, oCallBack, toBody) {
	var othis = this;
    var soap = new HWSOAP(this._url);
    if (toBody) {
        soap.sendb(oData, true, function(oResult){
            if (oResult && !oResult.status) {
                soap.showError(oResult.error);
            }
            oCallBack(oResult);
        });
    } else {
        soap.send(oData, true, function(oResult){
            if (oResult && !oResult.status) {
                soap.showError(oResult.error);
            }
            oCallBack(oResult);
        });
    }
}

/**
 * 查询数据
 * @param oParams 参数对象  {rownum:10,start:1,rowcount:true,params:{'xm':'张','xb':1}}
 * @param bAsync 是否异步,默认为否
 * @param oCallBack 异步回掉方法
 * @param unQuery 不查询直接注入数据
 * @returns {*}
 *  如果rowcount = true {count:100, records:[record:{id:'',pid:'',title:''},record:{...}]}
 *  如果rowcount = false [record:{id:'',pid:'',title:''},record:{...}]
// */
DataService.prototype.queryData = function(oParams, bAsync, oCallBack, unQuery) {
	if(oParams) this._param = oParams;
    //是否从本地加载
    var bLocal = false;
    if (this._control) {
        if (this._control.getAttribute().local && this._control.getAttribute().local == "true") {
            bLocal = true;
        }
    }
    if (this._url != "" && !unQuery) {
        var soap = new HWSOAP(this._url, this.getComponent());
        oParams = this.formatActionRequest(oParams);
        if (bAsync) {
            var othis = this;
            if (bLocal) {
                soap.local(oParams, true, function(oResult){
                    othis.setData(oResult);
                    oCallBack(oResult);
                });
            } else {
                soap.send(oParams, true, function(oResult){
                    othis.setData(oResult);
                    oCallBack(oResult);
                });
            }
        } else {
            if (bLocal) {
                soap.local(oParams);
            } else {
                soap.send(oParams);
            }
            var oResult = soap.getResult();
            this.setData(oResult);
            if (oCallBack) {
            	oCallBack(oResult);
            }
            return oResult;
        }
    } else {
        if (bAsync && oCallBack) {
            oCallBack(this.getData());
        } else {
            return this.getData();
        }
    }
}

/**
 * 导出excel，返回服务器生成的excel文件web路劲地址
 * @param {Object} oParams
 */
DataService.prototype.exportExcel = function(oParams,iTotalCount) {
	if(typeof(oParams) == "string"){
		oParams = new Object();
	}
	var iMaxExcelCount=10000;//每个Excel文件放置的最大记录个数
	var iTotalExcel = Math.floor(iTotalCount / iMaxExcelCount);
    if(iTotalCount%iMaxExcelCount != 0){//不能被整除
    	iTotalExcel++;
    }
    if(iTotalExcel>1){//多个才提示
    	if(!confirm("导出的数据过多，将分文件导出,确定吗？")){
    		return;
    	}	
    }    
    this.exportExcelByOne(iMaxExcelCount,0,iTotalExcel,oParams);
}
/**
 *分文件导出
 */
DataService.prototype.exportExcelByOne = function(rownum,iExcelIndex,iTotalExcel,oParams){
	if(iExcelIndex<iTotalExcel){
		//什么也不做
	}else{
		if(iTotalExcel>1){
			alert("文件全部下载完毕");
		}
		return;//导出文件结束
	}
	if(iTotalExcel>1){
		alert("即将导出第"+(iExcelIndex+1)+"个文件，总共"+iTotalExcel+"个文件");
	}
	var soap = new HWSOAP(this._url);
    var oRequest = new Object();
	oRequest.rownum = rownum;
    oRequest.start = iExcelIndex * rownum;
    oRequest.params = oParams;
    oRequest.excel = true;
    var that=this;
	soap.send(oRequest,true,function(oResult){
		if (null == oResult) {
        	throw new Error(1,"ServerData.getDataPage:server query error.\n");
    	}
		$("#downfile").remove();
		var downfile = document.createElement("iframe");	
		downfile.id = "downfile";
		$(downfile).css({width:0,height:0,display:"none"});	
		$("body").append(downfile);
		downfile.src = "/file/getfile.do?path=" + oResult.path + "";
		iExcelIndex++;
		that.exportExcelByOne(rownum,iExcelIndex,iTotalExcel,oParams);
	});    
}

/**
 * 格式化带动作请求
 */
DataService.prototype.formatActionRequest = function(oParams) {
    if (this._action) {
        var oReq = {};
        oReq.action = this._action;
        oReq.data = oParams;
        oParams = JSON.stringify(oReq);
    }
    return oParams;
}

/**
 * 得到3.0编码值
 */
DataService.prototype.getCode = function(permission) {
	var obj = {};
	var soap = new HWSOAP2("/hwsvr2/qe");
	soap.addRequest("query_code", "<permission>"+permission+"</permission>", "");
	soap.send("/hwsvr2/qe");
	var sr = soap.getResult(0);
	if(sr != null){
		obj.dm = $(sr).find("dm").text();
		obj.ma = $(sr).find("ma").text();
	}
	return obj;
}

